<template>
  <div class="message-layout">
    <!-- 左侧侧边栏 -->
    <ChatSidebar 
      :user="{}" 
      :history-list="historyList"
      @select-history="handleHistorySelect"
      @delete-history="deleteHistory"
      @clear-history="clearHistory"
      @view-all="viewAllHistory"
      @replay="replayTask"
      @download="downloadReport"
      @download-all="downloadAllReports"
      @download-stats="downloadReportStats"
    />

    <!-- 主聊天界面 -->
    <main class="chat-container">
      <!-- 头部 -->
      <header class="chat-header">
        <div class="chat-title">
          <div class="status-indicator">
            <span class="status-dot"></span>
            <span class="status-pulse"></span>
          </div>
          <span class="title-text">{{ conversation?.title || '新对话' }}</span>
        </div>
        <div class="header-actions">
          <button @click="resetConversation" class="action-btn" title="清空当前屏幕">
            <font-awesome-icon icon="rotate-right" />
          </button>
        </div>
      </header>

      <!-- 消息列表 -->
      <div class="messages-area" ref="scrollContainer">
        <!-- 空状态 -->
        <div v-if="messages.length === 0 && !isLoading && !shouldAutoSend" class="empty-state">
          <div class="empty-icon">
            <font-awesome-icon icon="robot" />
          </div>
          <h2>有什么我可以帮您的吗？</h2>
          <p>尝试提问关于任务执行、数据分析或生成报告的问题。</p>
          <div class="suggestion-chips">
            <button @click="inputContent = '分析上周的销售数据'; textareaRef?.focus()">📈 分析销售数据</button>
            <button @click="inputContent = '生成一份系统健康报告'; textareaRef?.focus()">📑 生成系统报告</button>
          </div>
        </div>

        <!-- 加载状态 -->
        <div v-else-if="isLoading || shouldAutoSend" class="loading-overlay">
          <div class="loading-spinner">
            <font-awesome-icon icon="spinner" spin />
          </div>
          <p>{{ shouldAutoSend ? '正在准备自动发送...' : '正在同步消息...' }}</p>
        </div>

        <!-- 消息流 -->
        <TransitionGroup name="message-fade" tag="div" class="message-list">
          <div 
            v-for="message in messages" 
            :key="message.id" 
            class="message-row" 
            :class="message.role"
          >
            <div class="message-inner">
              <!-- 头像 -->
              <div class="msg-avatar">
                <font-awesome-icon :icon="message.role === 'user' ? 'user' : 'robot'" />
              </div>

              <!-- 消息内容体 -->
              <div class="msg-content-wrapper">
                
                <!-- 1. 文本内容 (支持 HTML/Markdown) -->
                <!-- 注意：如果没有 Markdown 转换器，改回 {{ message.content }} 并加上 class="pre-wrap" -->
                <div v-if="message.content" class="text-content markdown-body" v-html="renderMessageContent(message.content)"></div>
                
                <!-- 正在思考/执行占位 -->
                <div v-else-if="message.role === 'assistant' && isSending && (!message.steps || message.steps.length === 0)" class="thinking-placeholder">
                  <span class="dot"></span><span class="dot"></span><span class="dot"></span>
                </div>

                <!-- 2. 工具执行过程 (仅 AI) -->
                <div v-if="message.steps && message.steps.length > 0" class="execution-cards">
                  <div 
                    v-for="card in mergeStepsToCards(message.steps)" 
                    :key="card.stepId" 
                    class="step-card"
                    :class="{ 'collapsed': collapsedSteps[card.stepId] }"
                  >
                    <!-- 卡片头部 (可点击折叠) -->
                    <div class="card-header" @click="toggleStepCard(card.stepId)">
                      <div class="header-left">
                        <div class="step-badge">{{ card.stepId }}</div>
                        <span class="tool-name">{{ card.tool_name || 'Thinking' }}</span>
                      </div>
                      <div class="header-right">
                        <span class="status-tag" :class="card.status">
                          <font-awesome-icon v-if="card.status === 'running'" icon="spinner" spin />
                          <font-awesome-icon v-else-if="card.status === 'completed'" icon="check" />
                          <font-awesome-icon v-else-if="card.status === 'failed'" icon="circle-exclamation" />
                          {{ getStatusText(card.status) }}
                        </span>
                        <font-awesome-icon icon="chevron-down" class="collapse-icon" />
                      </div>
                    </div>

                    <!-- 卡片内容 (折叠区域) -->
                    <div class="card-body" v-show="!collapsedSteps[card.stepId]">
                      <!-- 思考 -->
                      <div v-if="card.thought" class="section thought-section">
                        <div class="section-icon"><font-awesome-icon icon="lightbulb" /></div>
                        <div class="section-content">{{ card.thought }}</div>
                      </div>

                      <!-- 参数 -->
                      <div v-if="card.args" class="section code-section">
                        <div class="code-label">Input</div>
                        <div class="code-box">{{ formatArgs(card.args) }}</div>
                      </div>

                      <!-- 结果 -->
                      <div v-if="card.result || card.images?.length" class="section result-section">
                        <div class="code-label">Output</div>
                        <div v-if="card.images && card.images.length > 0" class="image-grid">
                           <img 
                              v-for="(img, i) in card.images" 
                              :key="i" 
                              :src="img" 
                              class="result-img" 
                              @click.stop="previewImage(img)" 
                              alt="Result"
                            />
                        </div>
                        <div v-if="card.result" class="section-content result-text">
                          {{ card.result }}
                        </div>
                      </div>
                    </div>
                  </div>
                </div>

                <!-- 3. 错误信息 -->
                <div v-if="message.content && message.content.includes('【MCP客户端】')" class="error-message">
                  <font-awesome-icon icon="triangle-exclamation" />
                  <span>{{ message.content }}</span>
                </div>
              </div>
            </div>
          </div>
        </TransitionGroup>
        
        <!-- 滚动锚点 -->
        <div ref="messagesEndRef" class="scroll-anchor"></div>
      </div>

      <!-- 底部输入区 -->
      <div class="input-wrapper">
        <div class="input-container">
          <div class="input-box" :class="{ 'is-focused': isInputFocused }">
            <textarea 
              v-model="inputContent"
              placeholder="发送消息..."
              @keydown.enter.exact.prevent="handleSendMessage"
              @keydown.shift.enter.exact="handleLineBreak"
              @focus="isInputFocused = true"
              @blur="isInputFocused = false"
              rows="1"
              ref="textareaRef"
            ></textarea>
            
            <div class="input-actions">
              <div class="tool-buttons">
                <button class="icon-btn" title="上传附件"><font-awesome-icon icon="paperclip" /></button>
                <button class="icon-btn" title="上传图片"><font-awesome-icon icon="image" /></button>
              </div>
              
              <button 
                class="send-btn"
                :disabled="!inputContent.trim() || isSending"
                @click="handleSendMessage"
              >
                <font-awesome-icon v-if="isSending" icon="spinner" spin />
                <font-awesome-icon v-else icon="paper-plane" />
              </button>
            </div>
          </div>
          <div class="footer-note">
            AI 生成内容仅供参考，请以实际环境为准。
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch, onUnmounted, nextTick } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useChatStore } from '@/stores/chatStore'
import { useChatStream } from '@/composables/useChatStream'
import { extractImageUrls, previewImage } from '@/utils/imageHelper'
import type { StepItem, StepCard } from '@/types/chat'
import ChatSidebar from '@/components/layout/ChatSidebar.vue'

// 如果项目中安装了 markdown-it，建议引入
// import MarkdownIt from 'markdown-it';
// const md = new MarkdownIt();

const chatStore = useChatStore()
const router = useRouter()
const route = useRoute()

const { sendMessage, cleanup } = useChatStream()

// --- 本地状态 ---
const inputContent = ref('')
const textareaRef = ref<HTMLTextAreaElement | null>(null)
const messagesEndRef = ref<HTMLElement | null>(null)
const shouldAutoSend = ref(false)
const autoSendContent = ref('')
const isInputFocused = ref(false)

// 用于管理工具卡片的折叠状态 (key: stepId, value: boolean)
const collapsedSteps = ref<Record<number, boolean>>({})

// --- 计算属性 ---
const historyList = computed(() => chatStore.historyList)
const conversation = computed(() => chatStore.currentConversation)
const messages = computed(() => chatStore.messages)
const isLoading = computed(() => chatStore.isLoading)
const isSending = computed(() => chatStore.isSending)

// --- 监听与生命周期 ---
watch(() => route.query.conversationId, async (newId) => {
  if (newId) {
    await chatStore.loadConversation(Number(newId))
    if (route.query.autoSend) {
      shouldAutoSend.value = true
      autoSendContent.value = route.query.autoSend as string
      router.replace({ query: { conversationId: newId } })
    }
  } else {
    chatStore.reset()
  }
}, { immediate: true })

// 监听消息列表变化，自动滚动到底部
watch(() => chatStore.messages, async (newMessages, oldMessages) => {
  // 检查自动发送
  if (shouldAutoSend.value && autoSendContent.value && newMessages.length === 0 && !chatStore.isLoading) {
    await handleAutoSend()
  }
  // 滚动到底部
  if (newMessages.length !== oldMessages?.length) {
    await scrollToBottom()
  }
}, { deep: true })

// 监听正在生成内容时，跟随滚动
watch(() => messages.value[messages.value.length - 1]?.content, () => {
    scrollToBottom()
})

// --- 方法 ---

// 渲染 Markdown (如果没有 markdown-it，直接返回 text)
const renderMessageContent = (text: string) => {
  if (!text) return ''
  // return md.render(text); // 推荐使用 markdown-it
  return text.replace(/\n/g, '<br>') // 简单的换行处理作为降级方案
}

const scrollToBottom = async () => {
  await nextTick()
  if (messagesEndRef.value) {
    messagesEndRef.value.scrollIntoView({ behavior: 'smooth', block: 'end' })
  }
}

const handleAutoSend = async () => {
  if (!autoSendContent.value || chatStore.isSending) return
  const content = autoSendContent.value
  autoSendContent.value = ''
  shouldAutoSend.value = false
  await sendMessage(content)
}

const handleSendMessage = async () => {
  const content = inputContent.value.trim()
  if (!content) return

  inputContent.value = ''
  adjustTextareaHeight()

  if (!chatStore.currentConversationId) {
    try {
      const newConv = await chatStore.startNewConversation(content.substring(0, 50), content)
      await router.replace({ query: { conversationId: newConv.id } })
    } catch (e) { return }
  }

  await sendMessage(content)
  await scrollToBottom()
}

// 切换卡片折叠
const toggleStepCard = (stepId: number) => {
  collapsedSteps.value[stepId] = !collapsedSteps.value[stepId]
}

const mergeStepsToCards = (steps: StepItem[]): StepCard[] => {
  if (!steps || steps.length === 0) return []
  const stepMap = new Map<number, StepCard>()
  
  steps.forEach(item => {
    if (!stepMap.has(item.step)) {
      stepMap.set(item.step, {
        stepId: item.step, thought: '', tool_name: '', args: null, result: '', status: 'pending', images: []
      })
    }
    const card = stepMap.get(item.step)!
    
    if (item.type === 'thought') card.thought = (card.thought + ' ' + (item.content || '')).trim()
    if (item.type === 'tool_call') {
      card.tool_name = item.tool_name || 'System Tool'
      card.args = item.arguments
      card.status = item.status || 'running'
    }
    if (item.type === 'tool_result') {
      card.result = item.result || ''
      card.status = (item.status as any) || 'completed'
      if (item.result && typeof item.result === 'string') {
        const imageUrls = extractImageUrls(item.result)
        if (imageUrls.length) card.images = [...(card.images || []), ...imageUrls]
      }
      if (item.images?.length) card.images = [...(card.images || []), ...item.images]
    }
  })
  
  const result = Array.from(stepMap.values()).sort((a, b) => a.stepId - b.stepId)
  
  // 默认折叠之前的步骤，只展开最后一个正在运行或刚完成的
  result.forEach((card, index) => {
    if (collapsedSteps.value[card.stepId] === undefined) {
      // 这里的逻辑：如果不是最后一个，且状态是完成的，默认折叠
      const isLast = index === result.length - 1
      collapsedSteps.value[card.stepId] = !(isLast || card.status === 'running' || card.status === 'failed')
    }
  })

  return result
}

const formatArgs = (args: any) => {
  try { return JSON.stringify(args, null, 2) } catch { return String(args) }
}

const getStatusText = (status: string) => {
  const map: Record<string, string> = { 'pending': '等待中', 'running': '执行中', 'completed': '已完成', 'failed': '执行失败' }
  return map[status] || status
}

const handleLineBreak = () => {
  inputContent.value += '\n'
  adjustTextareaHeight()
}

const adjustTextareaHeight = () => {
  if (textareaRef.value) {
    textareaRef.value.style.height = 'auto'
    textareaRef.value.style.height = Math.min(textareaRef.value.scrollHeight, 200) + 'px'
  }
}

// Sidebar handlers (保持原样)
const handleHistorySelect = (id: number) => chatStore.switchToHistory(id)
const deleteHistory = async (id: any) => { try { await chatStore.deleteHistoryRecord(id) } catch (e) {} }
const clearHistory = async () => { if (confirm('确定要清空？')) await chatStore.clearAllHistory() }
const viewAllHistory = () => {}
const replayTask = () => {}
const downloadReport = () => {}
const downloadAllReports = () => {}
const downloadReportStats = () => {}
const resetConversation = () => chatStore.setMessages([])

// 组件卸载时清理
onUnmounted(() => {
  cleanup()
})
</script>

<style scoped>
/* --- 全局变量与基础 --- */
.message-layout {
  --primary-color: #E60027;
  --primary-hover: #c40022;
  --primary-bg: rgba(230, 0, 39, 0.08);
  --ai-color: #10a37f;
  --bg-color: #f7f9fb;
  --card-bg: #ffffff;
  --text-primary: #1f2937;
  --text-secondary: #6b7280;
  --border-color: #e5e7eb;
  
  display: flex;
  height: 100vh;
  width: 100vw;
  background-color: var(--bg-color);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text-primary);
}

.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg-color);
  min-width: 0;
}

/* --- 自定义滚动条 --- */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 3px;
}
::-webkit-scrollbar-track {
  background: transparent;
}

/* --- 头部 --- */
.chat-header {
  height: 64px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(0,0,0,0.05);
  z-index: 10;
}

.chat-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-indicator {
  position: relative;
  width: 8px;
  height: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-dot {
  width: 8px;
  height: 8px;
  background: var(--ai-color);
  border-radius: 50%;
  position: absolute;
  z-index: 2;
}

.status-pulse {
  width: 8px;
  height: 8px;
  background: var(--ai-color);
  border-radius: 50%;
  position: absolute;
  opacity: 0.5;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); opacity: 0.5; }
  100% { transform: scale(3); opacity: 0; }
}

.title-text {
  font-weight: 600;
  font-size: 16px;
  color: var(--text-primary);
}

.action-btn {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.action-btn:hover { background: #f3f4f6; color: var(--text-primary); }

/* --- 消息区域 --- */
.messages-area {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0 0 0;
  display: flex;
  flex-direction: column;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding-bottom: 160px; /* 底部留白 */
}

/* 消息行基础 */
.message-row {
  width: 100%;
  padding: 0 24px;
  display: flex;
  justify-content: center;
}

.message-inner {
  width: 100%;
  max-width: 840px;
  display: flex;
  gap: 16px;
  align-items: flex-start;
}

/* 头像 */
.msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}
.user-avatar .msg-avatar { background: var(--primary-color); color: white; box-shadow: 0 4px 10px rgba(230,0,39,0.2); }
.ai-avatar .msg-avatar { background: var(--ai-color); color: white; box-shadow: 0 4px 10px rgba(16,163,127,0.2); }
/* 对应 template 中的 class 判断 */
.message-row.user .msg-avatar { background: var(--primary-color); color: white; }
.message-row.assistant .msg-avatar { background: var(--ai-color); color: white; }

/* 内容容器 */
.msg-content-wrapper {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 2px;
}

/* 文本气泡 */
.text-content {
  font-size: 15px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

/* AI 文本 */
.message-row.assistant .text-content {
  color: var(--text-primary);
}

/* User 文本 (气泡风) */
.message-row.user .message-inner { flex-direction: row-reverse; }
.message-row.user .msg-content-wrapper { align-items: flex-end; }
.message-row.user .text-content {
  background: var(--primary-color);
  color: white;
  padding: 10px 16px;
  border-radius: 18px 2px 18px 18px; /* 类似聊天软件的圆角 */
  box-shadow: 0 2px 8px rgba(230,0,39,0.15);
  max-width: 100%;
}

/* 思考点动画 */
.thinking-placeholder {
  padding: 10px 0;
  display: flex;
  gap: 4px;
}
.dot {
  width: 6px;
  height: 6px;
  background: #cbd5e1;
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out both;
}
.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }
@keyframes bounce { 0%, 80%, 100% { transform: scale(0); } 40% { transform: scale(1); } }

/* --- 工具卡片优化 --- */
.execution-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 8px;
  width: 100%;
}

.step-card {
  background: #fff;
  border: 1px solid var(--border-color);
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.2s ease;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

.step-card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.05); border-color: #d1d5db; }

/* 卡片头部 */
.card-header {
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  background: #fafafa;
  border-bottom: 1px solid transparent;
  user-select: none;
}
.step-card:not(.collapsed) .card-header { border-bottom-color: var(--border-color); }

.header-left { display: flex; align-items: center; gap: 10px; }
.step-badge {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-secondary);
  background: #e5e7eb;
  padding: 2px 6px;
  border-radius: 4px;
}
.tool-name { font-weight: 600; font-size: 13px; color: #374151; }

.header-right { display: flex; align-items: center; gap: 12px; }
.collapse-icon { color: #9ca3af; transition: transform 0.3s; font-size: 12px; }
.step-card.collapsed .collapse-icon { transform: rotate(-90deg); }

/* 状态标签 */
.status-tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 500;
}
.status-tag.running { background: #eff6ff; color: #3b82f6; }
.status-tag.completed { background: #f0fdf4; color: #22c55e; }
.status-tag.failed { background: #fef2f2; color: #ef4444; }

/* 卡片内容区 */
.card-body { padding: 16px; font-size: 14px; background: #fff; }

.section { margin-bottom: 12px; }
.section:last-child { margin-bottom: 0; }

.thought-section {
  display: flex;
  gap: 10px;
  color: #4b5563;
  font-style: italic;
  background: #f8fafc;
  padding: 10px;
  border-radius: 8px;
}
.section-icon { color: #f59e0b; margin-top: 2px; }

.code-label {
  font-size: 11px;
  text-transform: uppercase;
  color: #9ca3af;
  margin-bottom: 4px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.code-box, .result-text {
  background: #1e293b;
  color: #e2e8f0;
  padding: 12px;
  border-radius: 8px;
  font-family: 'Menlo', 'Monaco', monospace;
  font-size: 12px;
  overflow-x: auto;
  white-space: pre-wrap;
  line-height: 1.5;
}
.result-text { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }

/* 图片网格 */
.image-grid { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.result-img { height: 80px; border-radius: 6px; cursor: zoom-in; border: 1px solid #e5e7eb; transition: transform 0.2s; }
.result-img:hover { transform: scale(1.05); }

/* --- 空状态 --- */
.empty-state {
  height: 60vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--text-primary);
}
.empty-icon {
  width: 80px;
  height: 80px;
  background: #fff;
  border-radius: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  color: var(--ai-color);
  box-shadow: 0 10px 25px rgba(0,0,0,0.05);
  margin-bottom: 24px;
}
.empty-state h2 { font-size: 20px; font-weight: 600; margin-bottom: 8px; }
.empty-state p { color: var(--text-secondary); margin-bottom: 32px; }
.suggestion-chips { display: flex; gap: 12px; }
.suggestion-chips button {
  background: #fff;
  border: 1px solid var(--border-color);
  padding: 8px 16px;
  border-radius: 20px;
  color: var(--text-primary);
  cursor: pointer;
  transition: all 0.2s;
  font-size: 13px;
}
.suggestion-chips button:hover { border-color: var(--primary-color); color: var(--primary-color); transform: translateY(-1px); }

/* --- 加载状态 --- */
.loading-overlay {
  text-align: center;
  padding: 40px;
  color: #999;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.loading-spinner {
  font-size: 24px;
  color: var(--ai-color);
}

/* --- 底部输入区 --- */
.input-wrapper {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, #fff 0%, #fff 60%, rgba(255,255,255,0) 100%);
  padding: 40px 24px 24px;
  z-index: 20;
  display: flex;
  justify-content: center;
}

.input-container {
  width: 100%;
  max-width: 840px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-box {
  background: #fff;
  border: 1px solid var(--border-color);
  border-radius: 16px;
  padding: 12px 16px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
  transition: all 0.2s cubic-bezier(0.25, 0.8, 0.25, 1);
  display: flex;
  flex-direction: column;
}

.input-box.is-focused {
  border-color: var(--primary-color);
  box-shadow: 0 8px 30px rgba(230, 0, 39, 0.12);
  transform: translateY(-2px);
}

textarea {
  width: 100%;
  border: none;
  outline: none;
  resize: none;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text-primary);
  background: transparent;
  max-height: 200px;
  font-family: inherit;
}

.input-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid #f3f4f6;
}

.tool-buttons { display: flex; gap: 4px; }
.icon-btn {
  background: transparent;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  color: #9ca3af;
  cursor: pointer;
  transition: color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { color: var(--text-primary); background: #f3f4f6; }

.send-btn {
  background: var(--primary-color);
  color: white;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}
.send-btn:hover:not(:disabled) { background: var(--primary-hover); transform: scale(1.05); }
.send-btn:disabled { background: #e5e7eb; cursor: not-allowed; }

.footer-note { text-align: center; font-size: 12px; color: #cbd5e1; }

/* --- 动画 --- */
.message-fade-enter-active { transition: all 0.4s ease-out; }
.message-fade-enter-from { opacity: 0; transform: translateY(10px); }

/* Markdown 样式修正 (如果使用) */
.markdown-body :deep(p) { margin-bottom: 0.5em; }
.markdown-body :deep(p:last-child) { margin-bottom: 0; }
.markdown-body :deep(pre) { background: #1e293b; color: #e2e8f0; padding: 12px; border-radius: 8px; overflow-x: auto; }
</style>